<?php


namespace app\plugin\controller\merchant\service;


use app\common\controller\AppCommon;

class MerchantService
{
    /**
     * 初始化数据库,todo 改成不用每次都检测
     * @param false $resetInstall 是否重新安装
     */
    public static function ini_sql($resetInstall = false)
    {
        if ($resetInstall) {
            AppCommon::execute('DROP TABLE IF EXISTS `' . table_name('plugin_merchant') . '`;');
            AppCommon::execute('DROP TABLE IF EXISTS `' . table_name('plugin_merchant_login_log') . '`;');
        }
        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_merchant') . "';")) {
            AppCommon::execute("CREATE TABLE `" . table_name('plugin_merchant') . "` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_name` varchar(32) NOT NULL DEFAULT '' COMMENT '昵称',
  `account` varchar(32) NOT NULL DEFAULT '' COMMENT '登录账号',
  `pwd` varchar(32) NOT NULL DEFAULT '' COMMENT '密码',
  `salt` varchar(10) NOT NULL DEFAULT '' COMMENT '随机盐',
  `add_time` int(10) DEFAULT '0',
  `up_time` int(10) unsigned DEFAULT '0',
  `user_desc` varchar(255) DEFAULT '' COMMENT '简单描述',
  `role_id` int(10) unsigned DEFAULT '0' COMMENT '角色编号',
  `uid` varchar(50) DEFAULT '' COMMENT '唯一标识',
  `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0-禁用，1-启用，-1-待审核，-2-已驳回',
  `pwd_err_count` tinyint(1) NOT NULL DEFAULT '0' COMMENT '密码错误次数',
  `loginCount` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '登录次数',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;");
        }

        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_merchant_login_log') . "';")) {
            AppCommon::execute("CREATE TABLE `" . table_name('plugin_merchant_login_log') . "` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` varchar(50) NOT NULL DEFAULT '',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0',
  `ip` varchar(20) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;");
        }

        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_merchant_credits') . "';")) {
            AppCommon::execute("CREATE TABLE `" . table_name('plugin_merchant_credits') . "` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` varchar(50) NOT NULL DEFAULT '' COMMENT '商家UID',
  `credit` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '余额',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `uid` (`uid`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商家账户表';");
        }

        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_merchant_credit_log') . "';")) {
            AppCommon::execute("CREATE TABLE `". table_name('plugin_merchant_credit_log') ."` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` varchar(50) NOT NULL DEFAULT '',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '操作时间',
  `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '交易说明',
  `before_num` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '交易前',
  `num` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '交易量',
  `after_num` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '交易后',
  `type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '操作类型，1-收入，2-支出',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `uid_type` (`uid`,`type`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商家额交易流水表';");
        }

        if (!AppCommon::query("SHOW TABLES LIKE '" . table_name('plugin_merchant_msg') . "';")) {
            AppCommon::execute("CREATE TABLE `". table_name('plugin_merchant_msg') ."` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `uid` varchar(50) NOT NULL DEFAULT '' COMMENT '商户标识uid',
  `title` varchar(128) NOT NULL DEFAULT '' COMMENT '消息标题',
  `content` varchar(2000) NOT NULL DEFAULT '' COMMENT '消息内容',
  `type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '0-普通消息，1-系统通知',
  `read_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '查看时间',
  `add_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '发送时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商家消息';");
        }
    }

}